#if UNITY_EDITOR

using System; 
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using XFABManager;


namespace XFGameFramework.MissionSystem
{
    public class CustomMissionConfigDataIconGUI : CustomPropertyGUI
    {

        private bool iconFoldout;
        private GUIContent iconContent;
        private GUIContent dropDownContent;
        private GUIContent dropDownItemContent;
        //private GUIContent iconImageContent;

        public override void OnPropertyGUI(SerializedProperty serializedProperty)
        {

            MissionConfigData data = serializedProperty.serializedObject.targetObject as MissionConfigData;

            if (data == null) return;

            IconInfo info = data.icon;

            if (iconContent == null)
                iconContent = new GUIContent("Icon", "任务图标");

            if (dropDownContent == null)
                dropDownContent = new GUIContent();

            if (dropDownItemContent == null)
                dropDownItemContent = new GUIContent();

            EditorGUILayout.LabelField(string.Empty, "任务图标", "BoldLabel");
            iconFoldout = EditorGUILayout.Foldout(iconFoldout, iconContent);

            if (iconFoldout)
            {
                GUILayout.BeginHorizontal();

                GUILayout.Space(13);

                GUILayout.BeginVertical();

                XFABProject project = XFABProjectManager.Instance.GetProject(info.projectName);

                if (!string.IsNullOrEmpty(info.projectName))
                {
                    project = XFABProjectManager.Instance.GetProject(info.projectName);
                    if (project == null)
                    {
                        info.projectName = string.Empty;
                    }
                }

                dropDownContent.text = string.IsNullOrEmpty(info.projectName) ? "无" : info.projectName;

                GUILayout.BeginHorizontal();
                //GUILayout.Space(-0.5f);
                GUILayout.Label("projectName", GUILayout.ExpandWidth(false));
                GUILayout.Space(70);
                if (EditorGUILayout.DropdownButton(dropDownContent, FocusType.Passive))
                {
                    GenericMenu menu = new GenericMenu();

                    int count = XFABProjectManager.Instance.Projects.Count;

                    if (count != 0)
                    {
                        foreach (var item in XFABProjectManager.Instance.Projects)
                        {
                            GUIContent content = new GUIContent();
                            content.text = item.name;
                            menu.AddItem(content, info.projectName == item.name, () =>
                            { 
                                info.projectName = item.name;
                                EditorUtility.SetDirty(data); 
                                AssetDatabase.SaveAssets();
                                AssetDatabase.Refresh();

                            });
                        }
                    }
                    else
                    {
                        menu.AddDisabledItem(new GUIContent("无"));

                        menu.AddSeparator(string.Empty);

                        dropDownItemContent.text = "新建模块";
                        dropDownItemContent.tooltip = "未查询到资源模块,请创建后重试!";
                        menu.AddItem(dropDownItemContent, false, () =>
                        {
                            Type type = AssemblyTools.GetType("XFABManager.XFAssetBundleManagerProjects");
                            EditorWindow window = EditorWindow.GetWindow(type,false,"XFABManager");
                            window.Show();
                        });
                    }

                    menu.ShowAsContext();
                }

                GUILayout.EndHorizontal();
                 
                info.iconName = EditorGUILayout.TextField("iconName", info.iconName);
 
                GUILayout.EndVertical();

                GUIContent icon_content = EditorAssetTool.GetTextureGUIContent(info.projectName, info.iconName);
                 
                if (GUILayout.Button(icon_content, GUILayout.Width(39), GUILayout.Height(39)))
                { 
                    EditorGUIUtility.PingObject(icon_content.image); 
                }
                 
                GUILayout.EndHorizontal();
            }
        }
         
    }

}

#endif

